home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / tcqbsnip.zip / GETARG.BAS < prev    next >
BASIC Source File  |  1997-06-20  |  4KB  |  123 lines

  1. ' GETARG.BAS
  2. ' by Tika Carr
  3. ' October 28, 1996
  4. '
  5. ' ExePath$ written by Joe Negron
  6. '
  7. ' Donated to the public domain
  8. ' No warranties or guarantees are expressed or implied.
  9. '
  10. ' Purpose: Returns list of arguements from COMMAND$ to an array.
  11. ' REQUIRES QuickBasic 4.5! (Will not work in QBasic)
  12. ' (Start QuickBasic with: QB /L QB.QLB)
  13. '
  14. 'Description/Instructions:
  15. '
  16. 'Parses the COMMAND$ (which holds any parameters passed to the program
  17. 'from the command line). Returns the full path and filename of the
  18. 'program currently running in the global variable arg$(0). arg$(1) to
  19. 'arg$(85) holds each the command line parameters passed. Simply look for
  20. 'one of the arrays to be blank (ie. arg$(x)="") for the end of the list.
  21. 'if arg$(1)="" then there were no parameters passed to the program (here
  22. 'you would have it print a usage or help screen).
  23.  
  24. 'To test this program:
  25. '
  26. 'Set COMMAND$ for something like: test /sw1 /er /x -s Macro
  27. 'Then run the program below. After that, try passing no paramteres by
  28. 'setting COMMAND$ to "" or nothing. This is done in the IDE from the RUN
  29. 'menu, Modify COMMAND$ option.
  30.  
  31.  
  32. DECLARE SUB GetArg ()
  33. DECLARE FUNCTION ExePath$ ()
  34. '$INCLUDE: 'qb.bi'
  35.  
  36. DEFINT A-Z
  37.  
  38. DIM SHARED arg$(0 TO 85)
  39.  
  40. CLS
  41. GetArg
  42. PRINT "The program running is: "; arg$(0)
  43. 'Note that the above line may give the full path and file name to the
  44. 'QuickBasic 4.5 IDE program if you don't compile this. But when put in
  45. 'A library and it is compiled, it will show the correct executable and
  46. 'path.
  47.  
  48. IF arg$(1) = "" THEN
  49.   PRINT "You didn't pass any parameters"
  50. ELSE
  51.   i = 1
  52.   DO UNTIL arg$(i) = ""
  53.     PRINT "Parameter #"; i; " is: "; arg$(i)
  54.     i = i + 1
  55.   LOOP
  56. END IF
  57. PRINT i - 1; " parameters were found, excluding the name of the program."
  58.  
  59. 'EXEPATH$.BAS: Gets the path and filename of the executable running.
  60. '              by Joe Negron. Function name changed by Tika Carr.
  61. '-----------------------------------------------------------------------
  62. 'Function Description:
  63. '
  64. 'Uses DOS ISR 21H, Function 51H (Get PSP Address) to return the
  65. 'name of the currently executing program.  Note that this FUNCTION
  66. 'requires DOS 3.0 or >.
  67. '-----------------------------------------------------------------------
  68. '**** TEST Program ****
  69. 'E$ = ExePath$
  70. 'PRINT E$
  71. '-----------------------------------------------------------------------
  72. FUNCTION ExePath$ STATIC
  73.    DIM Regs AS RegType                       'Allocate space for TYPE
  74.                                              '  RegType
  75.    Regs.ax = &H5100                          'DOS function 51h
  76.    Interrupt &H21, Regs, Regs                '  Get PSP Address
  77.  
  78.    DEF SEG = Regs.bx                         'Regs.bx returns PSP sgmnt.
  79.    EnvSeg% = PEEK(&H2C) + PEEK(&H2D) * 256   'Get environment address
  80.    DEF SEG = EnvSeg%                         'Set environment address
  81.  
  82.    DO
  83.       Byte% = PEEK(Offset%)                  'Take a byte
  84.  
  85.       IF Byte% = 0 THEN                      'Items are ASCIIZ
  86.          Count% = Count% + 1                 '  terminated
  87.  
  88.          IF Count% AND EXEFlag% THEN         'EXE also ASCIIZ terminated
  89.             EXIT DO                          'Exit at the end
  90.          ELSEIF Count% = 2 THEN              'Last entry in env. is
  91.             EXEFlag% = -1                    '  terminated with two
  92.             Offset% = Offset% + 2            '  NULs.  Two bytes ahead
  93.          END IF                              '  is the EXE file name.
  94.       ELSE                                   'If Byte% <> 0, reset
  95.          Count% = 0                          '  zero counter
  96.  
  97.          IF EXEFlag% THEN                    'If EXE name found,
  98.             Temp$ = Temp$ + CHR$(Byte%)      '  build string
  99.          END IF
  100.       END IF
  101.  
  102.       Offset% = Offset% + 1                  'To grab next byte...
  103.    LOOP                                      'Do it again
  104.  
  105.    DEF SEG                                   'Reset default segment
  106.    ExePath$ = Temp$                      'Return value
  107.    Temp$ = ""                                'Clean up
  108. END FUNCTION
  109.  
  110. SUB GetArg
  111. arg$(0) = ExePath$
  112. IF COMMAND$ = "" THEN EXIT SUB
  113. i = 1: j = 1
  114. DO UNTIL i >= LEN(COMMAND$)
  115.   WHILE MID$(COMMAND$, i, 1) <> " " AND MID$(COMMAND$, i, 1) <> ""
  116.     arg$(j) = arg$(j) + MID$(COMMAND$, i, 1)
  117.     i = i + 1
  118.   WEND
  119.   i = i + 1: j = j + 1
  120. LOOP
  121. END SUB
  122.  
  123.